package com.webside.points.controller;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.ExpertProtocolEntity;
import com.webside.points.service.ExpertProtocolService;
import com.webside.points.wechat.UploadUtil;



@Controller
@Scope("prototype")
@RequestMapping("/expertProtocol/")
public class ExpertProtocolController {

	@Autowired
	ExpertProtocolService expertProtocolService;
	
	@RequestMapping("findAllUI")
	public String findAllUI(Model model){
		System.out.println("进入专家协议查询>>>");
		model.addAttribute("expertProtocol",expertProtocolService.findAll());
		
		return "serve/expertProtocol";
	}
	
	@RequestMapping("findExpertProtocol")
	public String findExpertProtocol(Model model){
		System.out.println("进入专家协议查询>>>");
		model.addAttribute("expertProtocol",expertProtocolService.findAll());
		
		return "service/expert-protocol";
	}
	
	@RequestMapping("insert")
	public String insert(ExpertProtocolEntity expertProtocolEntity){
		expertProtocolEntity.setStatus("1");
		expertProtocolService.insert(expertProtocolEntity);
		return "redirect:findAllUI";
	}
	
	@RequestMapping("update")
	public String update(ExpertProtocolEntity expertProtocolEntity){
		expertProtocolService.update(expertProtocolEntity);
		return "redirect:findAllUI";
	}
	
	@RequestMapping("uploadImage")
	public void uploadImage(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response){
		
		//上传图片
    	String image = UploadUtil.uploadImage(file, request);
    	System.out.println("保存路径：>>>"+image);
    	String json = JSON.toJSONString(image);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
	}

}
